﻿
using VXCompany.SilverlightLogging.MVVM.Models;
using System.Windows.Input;
namespace VXCompany.SilverlightLogging.MVVM.ViewModels
{
	/// <summary>
	/// ViewModel for Exception handling and logging.
	/// </summary>
	public class LogStatusControlViewModel : ViewModelBase
	{
		private ExceptionPolicy _ExceptionPolicy;

		/// <summary>
		/// Gets the current ExceptionPolicy.
		/// </summary>
		public ExceptionPolicy ExceptionPolicy
		{
			get { return _ExceptionPolicy; }
			private set
			{
				_ExceptionPolicy = value;
				RaisePropertyChanged("ExceptionPolicy");
			}
		}

		/// <summary>
		/// Returns ICommand that will clear all errormessages from Store without sync.
		/// </summary>
		public static ICommand ClearErrorsCmd
		{
			get;
			private set;
		}

		/// <summary>
		/// Creates new instance.
		/// </summary>
		public LogStatusControlViewModel()
		{
			if (LoggingApplicationService.Current != null)
			{
				this.ExceptionPolicy = LoggingApplicationService.Current.ExceptionPolicy;
			}

			ClearErrorsCmd = new DelegateCommand(() =>
			{
				if (ExceptionPolicy.Current != null
					&& ExceptionPolicy.Current.Store != null
					&& ExceptionPolicy.Current.Store.IsStorageAvailable)
				{
					ExceptionPolicy.Current.Store.Clear();
				}
			});
		}
	}
}
